package com.tfswx.jcyw.ruleenginedesigner.service.impl;

import com.tfswx.ruleengine.compute.exception.AppException;
import com.tfswx.ruleengine.compute.util.CommonUtil;
import com.tfswx.jcyw.ruleenginedesigner.dao.GzyqXtStdyMapper;
import com.tfswx.jcyw.ruleenginedesigner.dao.GzyqXtStjgMapper;
import com.tfswx.jcyw.ruleenginedesigner.dao.model.GzyqXtStdy;
import com.tfswx.jcyw.ruleenginedesigner.dao.model.GzyqXtStjg;
import com.tfswx.jcyw.ruleenginedesigner.dto.stgl.*;
import com.tfswx.jcyw.ruleenginedesigner.service.StglService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author Ricky
 * @date 2021/5/11
 */
@Service
public class StglServiceImpl implements StglService {

    @Resource
    private GzyqXtStdyMapper gzyqXtStdyMapper;
    @Resource
    private GzyqXtStjgMapper gzyqXtStjgMapper;

    @Override
    public List<StdyListByXmbmQueryOutputDTO> queryStdyListByXmbm(StdyListByXmbmQueryInputDTO stdyListByXmbmQueryInputDTO) {
        GzyqXtStdy gzyqXtStdy = new GzyqXtStdy();
        BeanUtils.copyProperties(stdyListByXmbmQueryInputDTO, gzyqXtStdy);
        return gzyqXtStdyMapper.queryStdyListByXmbm(gzyqXtStdy);
    }

    @Override
    public String addStdy(StdyAddInputDTO stdyAddInputDTO) {
        GzyqXtStdy gzyqXtStdy = new GzyqXtStdy();
        BeanUtils.copyProperties(stdyAddInputDTO, gzyqXtStdy);
        gzyqXtStdy.setStdybm(CommonUtil.getUuid());
        gzyqXtStdy.setSfsc("N");
        gzyqXtStdy.setCjsj(new Date());
        gzyqXtStdy.setZhxgsj(new Date());
        int insert = gzyqXtStdyMapper.insert(gzyqXtStdy);
        if (insert == 1) {
            return gzyqXtStdy.getStdybm();
        }
        throw new AppException("添加实体定义失败");
    }

    @Override
    public boolean updateStdy(StdyUpdateInputDTO stdyUpdateInputDTO) {
        GzyqXtStdy gzyqXtStdy = new GzyqXtStdy();
        BeanUtils.copyProperties(stdyUpdateInputDTO, gzyqXtStdy);
        gzyqXtStdy.setZhxgsj(new Date());
        int insert = gzyqXtStdyMapper.update(gzyqXtStdy);
        if (insert == 1) {
            return true;
        }
        throw new AppException("修改实体定义失败");
    }

    @Override
    public boolean deleteStdy(StdyDeleteInputDTO stdyDeleteInputDTO) {
        GzyqXtStdy gzyqXtStdy = new GzyqXtStdy();
        BeanUtils.copyProperties(stdyDeleteInputDTO, gzyqXtStdy);
        gzyqXtStdy.setZhxgsj(new Date());
        int insert = gzyqXtStdyMapper.delete(gzyqXtStdy);
        if (insert == 1) {
            return true;
        }
        throw new AppException("删除实体定义失败");
    }

    @Override
    public List<StjgListByStdybmQueryOutputDTO> queryStjgListByStdybm(StjgListByStdybmQueryInputDTO stjgListByStdybmQueryInputDTO) {
        GzyqXtStjg gzyqXtStjg = new GzyqXtStjg();
        BeanUtils.copyProperties(stjgListByStdybmQueryInputDTO, gzyqXtStjg);
        return gzyqXtStjgMapper.queryStjgListByStdybm(gzyqXtStjg);
    }

    @Override
    public String addStjg(StjgAddInputDTO stjgAddInputDTO) {
        GzyqXtStjg gzyqXtStjg = new GzyqXtStjg();
        BeanUtils.copyProperties(stjgAddInputDTO, gzyqXtStjg);
        gzyqXtStjg.setStjgbm(CommonUtil.getUuid());
        gzyqXtStjg.setSfsc("N");
        gzyqXtStjg.setCjsj(new Date());
        gzyqXtStjg.setZhxgsj(new Date());
        int insert = gzyqXtStjgMapper.insert(gzyqXtStjg);
        if (insert == 1) {
            return gzyqXtStjg.getStdybm();
        }
        throw new AppException("添加实体结构失败");
    }

    @Override
    public boolean updateStjg(StjgUpdateInputDTO stjgUpdateInputDTO) {
        GzyqXtStjg gzyqXtStjg = new GzyqXtStjg();
        BeanUtils.copyProperties(stjgUpdateInputDTO, gzyqXtStjg);
        gzyqXtStjg.setZhxgsj(new Date());
        int insert = gzyqXtStjgMapper.update(gzyqXtStjg);
        if (insert == 1) {
            return true;
        }
        throw new AppException("修改实体结构失败");
    }

    @Override
    public boolean deleteStjg(StjgDeleteInputDTO stjgDeleteInputDTO) {
        GzyqXtStjg gzyqXtStjg = new GzyqXtStjg();
        BeanUtils.copyProperties(stjgDeleteInputDTO, gzyqXtStjg);
        gzyqXtStjg.setZhxgsj(new Date());
        int insert = gzyqXtStjgMapper.delete(gzyqXtStjg);
        if (insert == 1) {
            return true;
        }
        throw new AppException("删除实体结构失败");
    }

    @Override
    public List<StTreeQueryOutputDTO> queryStTree(StTreeQueryInputDTO stTreeQueryInputDTO) {
        GzyqXtStdy gzyqXtStdy = new GzyqXtStdy();
        BeanUtils.copyProperties(stTreeQueryInputDTO, gzyqXtStdy);
        return gzyqXtStdyMapper.queryStTree(gzyqXtStdy);
    }

    @Override
    public int qkStjg(StjgQkInputDTO stjgQkInputDTO) {
        List<String> stdybmList = stjgQkInputDTO.getStdybmList();
        if (stdybmList == null || stdybmList.isEmpty()) {
            return 0;
        }
        int count = 0;
        for (int i = 0; i < stdybmList.size(); i += 500) {
            int end = i + 500;
            if (end > stdybmList.size()) {
                end = stdybmList.size();
            }
            List<String> strings = stdybmList.subList(i, end);
            count += gzyqXtStjgMapper.qkStjg(strings);
        }
        return count;
    }

    @Override
    public int qkStdy(StdyQkInputDTO stdyQkInputDTO) {
        List<String> xmbmList = stdyQkInputDTO.getXmbmList();
        if (xmbmList == null || xmbmList.isEmpty()) {
            return 0;
        }
        int count = 0;
        for (int i = 0; i < xmbmList.size(); i += 500) {
            int end = i + 500;
            if (end > xmbmList.size()) {
                end = xmbmList.size();
            }
            List<String> strings = xmbmList.subList(i, end);
            count += gzyqXtStdyMapper.qkStdy(strings);
        }
        return count;
    }
}
